package com.example.dezhou_puke.utils;

import com.example.dezhou_puke.entity.Puke;
import com.example.dezhou_puke.enums.Color;

import java.util.*;

/**
 * @ClassName: DeZhouPuKeUtils
 * @Description: TODO
 * @Author: YangGuisen
 * @Date: 2018/6/5 14:34
 * @Version: 1.0
 */
public class DeZhouPuKeUtils {

    private static Map<Integer, String> colorMap = new HashMap<>();

    static {
        colorMap.put(1, "红桃");
        colorMap.put(2, "黑桃");
        colorMap.put(3, "方块");
        colorMap.put(4, "梅花");
    }


    /**
     * 创建一手（5张）扑克牌
     *
     * @return
     */
    public static List<Puke> createFivePukes() {
        List<Puke> list = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        while (true) {
            Puke puke = createOnePuke();
            String colorAndNum = puke.getColor() + "_" + puke.getNum().toString();
            if (list.size() == 5) {
                break;
            }
            if (null != map.get(colorAndNum) && map.get(colorAndNum) == 1) {
                continue;
            } else {
                map.put(colorAndNum, 1);
                list.add(puke);
            }
        }
        return list;
    }


    /**
     * 创建一张扑克牌
     *
     * @return
     */
    public static Puke createOnePuke() {
        Puke puke = new Puke();
        puke.setNum(new Random().nextInt(13) + 2);
        puke.setColor(colorMap.get(new Random().nextInt(4) + 1));
        return puke;
    }

    /**
     * 判断是否是同花
     *
     * @param pukeList1
     * @return
     */
    public static boolean isSameColor(List<Puke> pukeList1) {
        Map<String, Puke> map = new HashMap<>();
        pukeList1.forEach(k -> {
            if (map.get(k.getColor()) == null) {
                map.put(k.getColor(), k);
            }
        });
        if (map.size() == 1) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 判断是否是顺子
     *
     * @param pukeList1
     */
    public static boolean isShunZi(List<Puke> pukeList1) {

        Collections.sort(pukeList1, Comparator.comparing(Puke::getNum));
        Integer sum = 0;
        for (Puke puke : pukeList1) {
            sum += puke.getNum();
        }

        Integer shunZiSum = new Double((pukeList1.get(0).getNum() + pukeList1.get(4).getNum()) * 2.5).intValue();
        if (sum.equals(shunZiSum)) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 校验手动输入的牌是否合法
     *
     * @param pukeList1
     * @return
     */
    public static boolean judgePuKesListIsAuthorized(List<Puke> pukeList1) {

        Map<String, Integer> colorAndNumMap = new HashMap<>();
        for (Puke k : pukeList1) {
            String colorAndNum = k.getColor() + k.getNum().toString();
            if (null != colorAndNumMap.get(colorAndNum) && colorAndNumMap.get(colorAndNum) == 1) {
                return false;
            } else {
                colorAndNumMap.put(colorAndNum, 1);
            }

        }
        return true;
    }


}
